package dd.prog2.exercises.set13.game;

import java.awt.Color;

import dd.prog2.exercises.set12.a2.Coin;

public class Game {

	public Coin[] buttons = new Coin[42];
	private int board_size = 42;
	private int player = 1;
	private int turnCounter = 0;
	private boolean gameOver = false;
	private int winner = 0;

	public Game() {}

	/*
	 * get coin at position i
	 */
	int get(int i){
		return buttons[i].color;
	}
	
	public void reset(){
		for (int i = 0; i < board_size; i++) {
			buttons[i].color = 0;
			buttons[i].repaint();
			player = 1;
			turnCounter = 0;
			gameOver = false;
			winner = 0;
			buttons[i].setDisabled();
			if(i>34)
				buttons[i].setEnabled(); // re-enable bottom buttons
			buttons[i].setText("");
		}
	}
	
	private void isGameOver() {
		// turnCounter=42? => draw
		if (turnCounter == 42) {
			winner = 0;
			gameOver = true;
		}
		// check winner horiz
//		System.out.println("horiz search:");
		if (!gameOver) {
			for (int i = 0; i < 6; i++) {
				for (int j = 0; j < 4; j++) {
					if (buttons[i*7+j].color != 0)
						if (buttons[i*7+j].color == buttons[i*7+j + 1].color
								&& buttons[i*7+j].color == buttons[i*7+j + 2].color
								&& buttons[i*7+j].color == buttons[i*7+j + 3].color) 
						{
							winner = buttons[i*7+j].color;
							gameOver = true;
						}
				}
			}
		}
		// check winner vertical
//		System.out.println("vertical search:");
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 7; j++) {
					if (buttons[i*7+j].color != 0)
						if (buttons[i*7+j].color == buttons[(i+1)*7+j].color
								&& buttons[i*7+j].color == buttons[(i+2)*7+j].color
								&& buttons[i*7+j].color == buttons[(i+3)*7+j].color) {
							winner = buttons[i*7+j].color;
							gameOver = true;
						}
				}
			}
		}
		// check winner diag right
//		System.out.println("diag right search:");
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 4; j++) {
					if (buttons[i*7+j].color != 0)
						if (buttons[i*7+j].color == buttons[(i+1)*7+j + 1].color
								&& buttons[i*7+j].color == buttons[(i+2)*7+j + 2].color
								&& buttons[i*7+j].color == buttons[(i+3)*7+j + 3].color) {
							winner = buttons[i*7+j].color;
							gameOver = true;
						}
				}
			}
		}
		// check winner diag left
//		System.out.println("diag left search:");
		if (!gameOver) {
			for (int i = 0; i < 3; i++) {
				for (int j = 3; j < 7; j++) {
					if (buttons[i*7+j].color != 0) {
						if (buttons[i*7+j].color == buttons[(i+1)*7+j - 1].color
								&& buttons[i*7+j].color == buttons[(i+2)*7+j - 2].color
								&& buttons[i*7+j].color == buttons[(i+3)*7+j - 3].color) {
							winner = buttons[i*7+j].color;
							gameOver = true;
						}

					}
				}
			}
		}
	}

	public void setTurn(int index) {
		boolean freeCellFound = false;
		do{
			if(!buttons[index].isEnabled)
				index += 7;
			else if(buttons[index].isEnabled){
				freeCellFound = buttons[index].isEnabled;
				break;
			}
		}while(index < 42);
		if(freeCellFound){
			if (index - 7 >= 0) //enable next(row) button
				this.buttons[index - 7].setEnabled();
			buttons[index].color = player;
			if (this.getPlayer() == 1) {
				buttons[index].setBackground(Color.RED);
			} else
				buttons[index].setBackground(Color.YELLOW);
			buttons[index].setDisabled();
			turnCounter++;
			if (player == 1)
				player = 2;
			else
				player = 1;
			if (turnCounter > 4)
				isGameOver();
			buttons[index].repaint();
		}
			// return "Success";
	}

	// getter
	public int getPlayer() {
		return player;
	}

	public int getWinner() {
		return winner;
	}

	public boolean getGameOver() {
		return gameOver;
	}
}
